package pwd.allen.demo;

import pwd.allen.demo.service.DBService;
import pwd.allen.password.PasswordUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.util.List;

/**
 * @author pwdan
 * @create 2022-01-14 15:56
 **/
public class MyPasswordUtil extends PasswordUtil {

    /**
     * 模拟从数据库中校验弱密码
     * @return
     */
    public boolean checkFromDB() {
        WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getWebApplicationContext(ServletRequestAttributes.class.cast(RequestContextHolder.getRequestAttributes()).getRequest().getServletContext());
        DBService dbService = webApplicationContext.getBean(DBService.class);
        List<String> listWeakPasswordFromDB = dbService.getWeakPasswordFromDB();
        if (!CollectionUtils.isEmpty(listWeakPasswordFromDB)) {
            return !listWeakPasswordFromDB.contains(password);
        }
        return true;
    }
}
